package com.example.farmeri.ui.strategy;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.farmeri.R;
import com.example.farmeri.tools.KeyboardUtils;
import com.example.farmeri.adapter.AccessPolicyAdapter;
import com.example.farmeri.adapter.MyItemDecoration;
import com.example.farmeri.common.OnExtraClickListener;
import com.example.farmeri.ui.base.BaseLinearRecycleViewActivity;
import com.google.android.material.bottomsheet.BottomSheetBehavior;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccessPolicyActivity extends BaseLinearRecycleViewActivity {

    OnExtraClickListener onExtraClickListener;
    List<String > select_list = new ArrayList<>();
    AccessPolicyAdapter adapter;
    Integer select_position;
    String  selectId;
    @BindView(R.id.sheet_bottom_checkedNum)
    TextView text_select_num;   //勾选的数目
    @BindView(R.id.sheet_bottom_del_multi)
    TextView del_multi; //批量删除


    //刷新
    @BindView(R.id.access_policy_srl)
    SwipeRefreshLayout srl;

    //取消搜索
    @BindView(R.id.search_cancer)
    TextView searchCancer;

    //搜索
    @BindView(R.id.search_edit)
    EditText searchEdit;

    //搜索结果
    @BindView(R.id.search_recycle)
    RecyclerView searchRecycle;

    //主页面
    @BindView(R.id.access_policy_layout_main)
    ViewGroup mainLayout;

    //搜索页面
    @BindView(R.id.access_policy_layout_search)
    ViewGroup searchLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_access_policy);
        ButterKnife.bind(this);
        initToolBar(R.id.access_policy_toolbar,"访问策略",true);
        //设置底部弹出框
        initBottomBehavior(R.id.sheet_bottom_container);
        //设置底部模态框
        initBottomSheetDialog(R.layout.dialog_model,"确定删除该策略吗");

        //accessPolicyService.getRoles(1,10);

        initSearchPager();

        //初始化访问策略的数据
        intRecycleView();

        initSingleOperate();

        //批量删除
        del_multi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        //刷新加载
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(AccessPolicyActivity.this,"加载中",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void intRecycleView() {
        adapter = new AccessPolicyAdapter(this,R.layout.item_access_policy,getScreeWidth());
        linearRecyclerViewAddData(R.id.access_policy_recycle_view,adapter, MyItemDecoration.SHORT_LINE);
        adapter.setOnExtraClickListener(new OnExtraClickListener() {

            @Override
            public void onLockClick(View v, String  id) {

            }


            @Override
            public void onEditClick(View v, String id) {

            }

            @Override
            public void onDelClick(View v, String id) {
                //select_position = 0;
                selectId = id;
                bottomSheetDialog.show();
            }

            @Override
            public void onUnChecked(int i) {
                select_list.remove(String.valueOf(i));
                if (select_list.isEmpty()){
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                }
                text_select_num.setText("已勾选"+select_list.size()+"个");
            }

            @Override
            public void onChecked(int i) {
                if (select_list.isEmpty()){
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                select_list.add(String.valueOf(i));
                text_select_num.setText("已勾选"+select_list.size()+"个");
            }
        });
    }

    /**
     * 初始化搜索页面
     */
    private void initSearchPager() {
        //搜索监听
        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    linearRecyclerViewAddData(R.id.search_recycle,
                            adapter,
                            MyItemDecoration.HIGH_LINE);
                    return true;
                }
                return false;
            }
        });

        //取消搜索
        searchCancer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchLayout.setVisibility(View.GONE);
                mainLayout.setVisibility(View.VISIBLE);
                searchRecycle.setAdapter(null);
                KeyboardUtils.hideKeyboard(searchEdit);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem item = menu.add("添加策略");
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        item.setIcon(R.drawable.ic_add_white_24dp);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(AccessPolicyActivity.this, AddStrategyActivity.class);
                startActivity(intent);
                return true;
            }
        });

        //添加搜索按钮
        MenuItem searchAction = menu.add("搜索");
        searchAction.setIcon(R.drawable.ic_search_white_24dp);
        searchAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        searchAction.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                mainLayout.setVisibility(View.GONE);
                searchLayout.setVisibility(View.VISIBLE);
                //设置输入内容为空
                searchEdit.setText("");
                //输入框获取焦点弹出软键盘
                KeyboardUtils.showKeyboard(searchEdit);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    TextView confirm;   //单选确定
    TextView cancer;    //单选返回
    protected void initSingleOperate() {
        //单选删除确定
        confirm = dialogView.findViewById(R.id.dialog_bottom_confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                select_list.remove(select_position);
                adapter.notifyItemRemoved(select_position);
                bottomSheetDialog.cancel();
            }
        });
        //单选删除取消
        cancer = dialogView.findViewById(R.id.dialog_bottom_cancer);
        cancer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.cancel();
            }
        });
    }

}
